package com.hk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.reggie.common.R;
import com.hk.reggie.entity.Category;
import com.hk.reggie.entity.Dish;
import com.hk.reggie.entity.Employee;
import com.hk.reggie.entity.Setmeal;
import com.hk.reggie.service.CategoryService;
import com.hk.reggie.service.SetmealDishService;
import com.hk.reggie.service.SetmealService;
import com.hk.reggie.vo.DishVo;
import com.hk.reggie.vo.SetmealVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealVo
     * @return
     */
    @PostMapping()
    public R<String> save(@RequestBody SetmealVo setmealVo){
        setmealService.saveWithDish(setmealVo);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        //构建分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealVo> setmealVoPage = new Page<>(page,pageSize);

        //构造条件构造器
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        //添加过滤条件
        if(!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        //添加排序条件
        queryWrapper.orderByDesc("update_time");

        //执行查询
        setmealService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, setmealVoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealVo> list = records.stream().map(record -> {
            SetmealVo setmealVo = new SetmealVo();
            //拷贝数据
            BeanUtils.copyProperties(record, setmealVo);
            //通过分类ID查询出分类名，被赋值给DishVo
            Long categoryId = setmealVo.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){
                setmealVo.setCategoryName(category.getName());
            }
            return setmealVo;
        }).collect(Collectors.toList());

        setmealVoPage.setRecords(list);

        return R.success(setmealVoPage);
    }

    /**
     * list
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        Long categoryId = setmeal.getCategoryId();
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.eq("status", setmeal.getStatus());
        List<Setmeal> setmeals = setmealService.list(queryWrapper);
        return R.success(setmeals);
    }


    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){
        List<Setmeal> setmeals = setmealService.list(new QueryWrapper<Setmeal>().in("id", ids));
        for(Setmeal setmeal : setmeals) {
            setmeal.setStatus(status);
            setmealService.updateById( setmeal);
        }
        return R.success("状态修改成功");
    }
}
